package edu.ycxy.poetry.controller;

import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.Writer;
import edu.ycxy.poetry.service.IWriterService;
import edu.ycxy.poetry.vo.AddPoemVo;
import edu.ycxy.poetry.vo.AddPoetsVo;
import edu.ycxy.poetry.vo.UpdatePoetVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@RestController
@RequestMapping("/writer")
@Slf4j
public class WriterController {
    @Autowired
    private IWriterService writerService;

    @RequestMapping("/{writerName}")
    public Writer findWriters(@PathVariable("writerName") String writerName){
        log.debug("查询的作者信息:{}"+writerService.findWriter(writerName));
        return writerService.findWriter(writerName);
    }

    // 查询所有的作者信息
    @RequestMapping("/selectList")
    public PageInfo<Writer> selectListAuthor(Integer pageNum){
        if (pageNum ==  null){
            pageNum = 1;
        }
        // 对查询出来的结果进行分页显示,每页10个数据
        PageInfo<Writer> pageInfo = writerService.selectListAuthor(pageNum, 10);
        log.debug("查询出来的诗人结果为:"+pageInfo);
        return pageInfo;
    }


    //根据作者的朝代信息查询相关的作者
    @RequestMapping("/selectList/{DyName}")
    public PageInfo<Writer> selectListByName(@PathVariable("DyName") String DyName, Integer pageNum){
        if (pageNum == null){
            pageNum =1;
        }
        PageInfo<Writer> pageInfo = writerService.selectListByName(DyName, pageNum, 10);
        log.debug("根据朝代名字查询出的作者结果:"+pageInfo);
        return pageInfo;
    }


    // 添加诗人信息
    @RequestMapping("/addPoets")
    public Integer AddPoets(@RequestBody AddPoetsVo addPoetsVo){
        log.debug("获取的诗人信息:" + addPoetsVo);
        Integer integer = writerService.addPoets(addPoetsVo);
        if (integer == 111){
            return 1;
        }else {
            return 2;
        }
    }

    @RequestMapping("/fuzzy/{search}")
    public PageInfo<Writer> findListByName(@PathVariable String search,Integer pageNum){
        if (pageNum == null){
            pageNum =1;
        }
        return writerService.findListByName(search,pageNum,10);
    }

    @RequestMapping("/delete/{id}")
    public void deleteById(@PathVariable Integer id){
        writerService.deleteById(id);
    }

    @RequestMapping("/findOne/{id}")
    public Writer findOneById(@PathVariable Integer id){
        return writerService.findOneById(id);
    }

    @RequestMapping("/update")
    public void updatePoet(@RequestBody UpdatePoetVo updatePoetVo){
        writerService.updatePoet(updatePoetVo);
    }
}
